package leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Project : AlgorithmLearning
 * @Package : Leetcode
 * @File : Q0046.java
 * @Author : WangRuoyu
 * @Date 2023/11/3 19:51
 */


public class Q0046 {
    int[] nums;
    boolean[] st;
    List<Integer> tmp = new ArrayList<>();
    List<List<Integer>> res = new ArrayList<>();

    private void dfs() {
        if (tmp.size() == nums.length) {
            res.add(new ArrayList<>(tmp));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (!st[i]) {
                st[i] = true;
                tmp.add(nums[i]);
                dfs();
                st[i] = false;
                tmp.remove(tmp.size() - 1);
            }
        }
    }

    public List<List<Integer>> permute(int[] nums) {
        this.nums = nums;
        this.st = new boolean[nums.length];
        dfs();
        return res;
    }

    public static void main(String[] args) {
        Q0046 sl = new Q0046();
        int[] nums = {1, 2, 3};
        System.out.println(sl.permute(nums));
    }
}
